<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';

const tags = ref([
  { id: 1, name: 'Vue.js', count: 15, color: '#4FC08D' },
  { id: 2, name: 'React', count: 12, color: '#61DAFB' },
  { id: 3, name: 'TypeScript', count: 20, color: '#3178C6' },
  { id: 4, name: 'JavaScript', count: 25, color: '#F7DF1E' },
  { id: 5, name: 'Node.js', count: 8, color: '#339933' },
  { id: 6, name: 'CSS', count: 18, color: '#1572B6' },
  { id: 7, name: 'HTML', count: 10, color: '#E34F26' },
  { id: 8, name: 'Python', count: 14, color: '#3776AB' },
  { id: 9, name: 'Java', count: 9, color: '#ED8B00' },
  { id: 10, name: 'Docker', count: 6, color: '#2496ED' },
  { id: 11, name: 'Git', count: 11, color: '#F05032' },
  { id: 12, name: 'Linux', count: 7, color: '#FCC624' },
  { id: 13, name: 'MySQL', count: 5, color: '#4479A1' },
  { id: 14, name: 'Redis', count: 4, color: '#DC382D' },
  { id: 15, name: 'Nginx', count: 3, color: '#009639' }
]);

const searchKeyword = ref('');
const selectedTags = ref([]);

const filteredTags = computed(() => {
  if (!searchKeyword.value) return tags.value;
  return tags.value.filter(tag => 
    tag.name.toLowerCase().includes(searchKeyword.value.toLowerCase())
  );
});

const totalArticles = computed(() => {
  return tags.value.reduce((sum, tag) => sum + tag.count, 0);
});

function toggleTag(tag: any) {
  const index = selectedTags.value.findIndex(t => t.id === tag.id);
  if (index > -1) {
    selectedTags.value.splice(index, 1);
  } else {
    selectedTags.value.push(tag);
  }
}

function isTagSelected(tag: any) {
  return selectedTags.value.some(t => t.id === tag.id);
}

function clearSelection() {
  selectedTags.value = [];
}

onMounted(() => {
  console.log('标签页面已加载');
});
</script>

<template>
  <div class="tags-page">
    <el-card class="mb-16px">
      <template #header>
        <div class="card-header">
          <span class="text-20px font-bold">文章标签</span>
          <div class="flex items-center gap-8px">
            <el-tag type="success">{{ filteredTags.length }} 个标签</el-tag>
            <el-tag type="info">{{ totalArticles }} 篇文章</el-tag>
          </div>
        </div>
      </template>
      <div class="text-gray-700 dark:text-gray-200">
        <p>这是标签测试页面，展示所有文章标签和相关统计信息。</p>
      </div>
    </el-card>

    <el-card class="mb-16px">
      <template #header>
        <div class="card-header">
          <span class="text-16px font-bold">搜索标签</span>
        </div>
      </template>
      <div class="flex items-center gap-12px">
        <el-input
          v-model="searchKeyword"
          placeholder="输入标签名称搜索..."
          clearable
          class="flex-1"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button v-if="selectedTags.length > 0" @click="clearSelection">
          清除选择 ({{ selectedTags.length }})
        </el-button>
      </div>
    </el-card>

    <el-card class="mb-16px">
      <template #header>
        <div class="card-header">
          <span class="text-16px font-bold">标签云</span>
        </div>
      </template>
      <div class="tag-cloud">
        <el-tag
          v-for="tag in filteredTags"
          :key="tag.id"
          :type="isTagSelected(tag) ? 'primary' : ''"
          :effect="isTagSelected(tag) ? 'dark' : 'light'"
          class="tag-item cursor-pointer m-4px"
          :style="{ 
            backgroundColor: isTagSelected(tag) ? tag.color : 'transparent',
            borderColor: tag.color,
            color: isTagSelected(tag) ? '#fff' : tag.color,
            fontSize: Math.min(16, 12 + tag.count / 3) + 'px'
          }"
          @click="toggleTag(tag)"
        >
          {{ tag.name }} ({{ tag.count }})
        </el-tag>
      </div>
    </el-card>

    <el-card v-if="selectedTags.length > 0">
      <template #header>
        <div class="card-header">
          <span class="text-16px font-bold">已选择的标签</span>
          <el-button size="small" @click="clearSelection">清除全部</el-button>
        </div>
      </template>
      <div class="selected-tags">
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-12px">
          <div 
            v-for="tag in selectedTags" 
            :key="tag.id"
            class="selected-tag-item p-12px border rounded-lg"
            :style="{ borderColor: tag.color }"
          >
            <div class="flex items-center justify-between">
              <div class="flex items-center">
                <div 
                  class="w-3 h-3 rounded-full mr-8px"
                  :style="{ backgroundColor: tag.color }"
                ></div>
                <span class="font-medium">{{ tag.name }}</span>
              </div>
              <el-tag size="small">{{ tag.count }} 篇</el-tag>
            </div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tag-cloud {
  line-height: 2;
}

.tag-item {
  transition: all 0.3s ease;
  margin: 4px;
}

.tag-item:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.selected-tag-item {
  transition: all 0.3s ease;
}

.selected-tag-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
</style> 